package control

import (
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"path"
	"pro_cla_sql/util"
	"time"
)

var Path string = ""

func Up(w http.ResponseWriter, r *http.Request) {
	buf, _ := ioutil.ReadFile(`up.html`)
	w.Write(buf)
}
func File(w http.ResponseWriter, r *http.Request) {
	w.Header().Set(`Content-Type`, `application/json`)
	w.Header().Set(`Access-Control-Allow-Origin`, `*`)
	fmt.Println("*******************************************")
	fmt.Println(r.Method)
	if r.Method == "OPTIONS" {
		w.WriteHeader(200)
		return
	}

	r.ParseMultipartForm(1 << 32)
	_, h, err := r.FormFile("file")
	fmt.Println(err)
	ext := path.Ext(h.Filename)
	dir := `res/img/` + time.Now().Format("2006-01-02") + "/" //yyyy-MM-rr
	os.MkdirAll(dir, 0666)
	f, _ := h.Open()
	name := util.RandStr() + ext
	println(dir + name)
	f1, _ := os.Create(dir + name)
	Path := "/" + dir + name
	println(Path)
	io.Copy(f1, f)
	f.Close()
	f1.Close()
	w.Write([]byte("/" + dir + name))
}
